package com.situ.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.entity.Emp;
import com.situ.service.EmpService;
import com.situ.test.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Emp的路由接口服务
 * 
 * @author胡文
 *
 */
//
@Api(tags = "员工管理")
@RestController
public class EmpController {

	/** EmpService服务 */
	@Autowired
	private EmpService empService;
	
	/**
	 * 查询所有Emp数据的方法
	 * @param value
	 * @return
	 */
    @ApiOperation(value = "带分页的查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页数" ,required = false),
            @ApiImplicitParam(name = "pageSize",value = "显示的条数默认5" ,required = false)
    })
    @Cacheable(value = "Emp",key = "#pageNum+'-'+#pageSize")
	@GetMapping(value = "/Emp/query", produces = {"application/json;charset=UTF-8"})
	public ResponseData query(Emp value,
                              @RequestParam(defaultValue = "1",required = false) int pageNum,
                              @RequestParam(defaultValue = "5", required = false) int pageSize
                              ) {

        PageHelper.startPage(pageNum,pageSize);
        List<Emp> list = empService.find(value);
        PageInfo<Emp> pageInfo=new PageInfo<>(list,5);

        return ResponseData.ok().
                putDataValue("list",list).putDataValue("pageInfo",pageInfo);
	}
	
	/**
	 * 通过id查询Emp数据的方法
	 * 
	 * @return
	 */
    @ApiOperation(value = "根据id查询 比如 $.getJSON('/Emp/queryById/1') 1就是id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "拼接在地址后面" ,required = true)
    })

    @GetMapping(value = "/Emp/queryById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData queryById(@PathVariable(name="id") Integer id) {
		 return ResponseData.ok().putDataValue("obj",empService.findOne(id));
	}
	
	/**
	 * 插入Emp属性不为空的数据方法
	 * 
	 * @return
	 */
    @ApiOperation(value = "添加emp")
	@PostMapping(value = "/Emp/save")
	public ResponseData save(Emp value) {
		return ResponseData.ok().putDataValue("obj", empService.saveNotNull(value));
	}
	
	/**
	 * 更新Emp属性不为空的数据方法
	 * 
	 * @return
	 */
    @ApiOperation(value = "修改emp")
	@PostMapping(value = "/Emp/update", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(Emp value) {
		return ResponseData.ok().putDataValue("obj",empService.updateNotNullById(value));
	}

	/**
	 * 通过id删除Emp数据方法
	 * 
	 * @return
	 */
    @ApiOperation(value = "根据id删除")
	@PostMapping(value = "/Emp/delById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delById(@PathVariable(name="id") Integer id) {
		return ResponseData.ok().putDataValue("obj",empService.deleteById(id));
	}


    //带分页的查询全部
//@ApiOperation("带分页的查询全部")
/*
    @RequestMapping(value = "/Emp/queryPage", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryPage(Emp value,
                             @RequestParam(defaultValue = "1",required = false) int pageNum ,
                             @RequestParam(defaultValue = "5",required = false) int pageSize) {

	    PageHelper.startPage(pageNum, pageSize);
        List<SysMight> list = sysMightService.find2(value);
    PageInfo pageInfo = new PageInfo<SysMight>(list,5);

        return ResponseData.ok()
        // .putDataValue("list", list)
        .putDataValue("pageInfo",pageInfo);
        }
 */
}
